<?php defined('SYSPATH') or die('No direct script access.');

class Model_Services_Medicine extends Model implements Jqgrid_GridListener{

	public $medicine;

	public function count($criteria = false){
		return ORM::factory("dao_medicine")->count_all();
	}
		
	public function getList($criteria = false){
			
		if (!$criteria)	$medicine_list = ORM::factory("dao_medicine")->find_all()->as_array();
		else{
			if ($criteria->isSearch){
				$medicine_list = ORM::factory("dao_medicine")
								->where($criteria->getSQLField("medicines"),$criteria->getSQLOper(), $criteria->getSQLSearchString())
								->order_by($criteria->sidx,$criteria->sord)
								->limit($criteria->limit)
								->offset($criteria->calcOffset())
								->find_all()
								->as_array();
						
			}else{
				$medicine_list = ORM::factory("dao_medicine")->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->offset($criteria->calcOffset())->find_all()->as_array(); 
			}
		} 

		$dataset = array();
		foreach ($medicine_list as $medicine){
			$dataset[$medicine->id] = array($medicine->name,$medicine->description);
		}
			
		return $dataset;
	}

	/**
		*
		* get all medicines from an id
		* @param int $minId
		* @param int $maxAmount
		* @return array of medicines
		*/
	public function getListFromMinID($minId, $maxAmount){
		return ORM::factory("dao_medicine")->where("id",">",$minId)->order_by("id", "ASC")->limit($maxAmount)->find_all()->as_array();
	}

	public function get($id){
		return ORM::factory("dao_medicine")->find($id);
	}

	public function getAll(){
		return ORM::factory("dao_medicine")->find_all()->as_array();
	}

	public function save($id, array $data){
		$this->medicine = ($id > 0) ? ORM::factory("dao_medicine",$id) : ORM::factory("dao_medicine");
		$this->medicine->name = $data['medicine_name'];
		$this->medicine->description = $data['description'];
		$this->medicine->save();
		return $this->medicine->id;
	}

	public function delete($id){
		$medicine = ORM::factory("dao_medicine",$id);
		$medicine->delete();
		return $id;
	}


	/**
		*
		* creturn an entity medicien by name
		* @param string $name
		* @return medicine 
		*/
	public function getByName($name){
		return ORM::factory("dao_medicine")->where("name","=",$name)->find();
	}


}



?>